package cn.stylefeng.guns.modular.business.frequency.statistics;

import cn.stylefeng.guns.core.pojo.response.ResponseData;
import cn.stylefeng.guns.modular.business.frequency.finance.model.result.PkgFinance;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description:
 * @Author jzy
 * @Date 2020/11/23 0023
 **/
@Api(tags = "数据统计")
@RestController
@RequestMapping("/api/statistics")
public class StatisticsController {

    @Resource
    private StatisticsService statisticsService;

    /**
     * @param month 不传默认当天
     * @return
     */
    @ApiOperation(value = "单日数据统计")
    @GetMapping("/daily/{month}")
    public ResponseData getDailyData(@PathVariable String month) {
        PkgDailyDataStatistics pdds = statisticsService.getDailyData(month);
        return ResponseData.andaSuccess(pdds);
    }

    @ApiOperation(value = "月度营业额、毛利统计")
    @GetMapping("/collection-gross/{month}")
    public ResponseData getMonthGrossData(@PathVariable String month) {
        List<PkgMonthDataStatistics> list = statisticsService.getMonthGrossData(month);
        Map<String, Object> map = new HashMap<>();
        if (list != null && list.size() > 0) {
            map.put("dDate", list.stream().map(PkgMonthDataStatistics::getDDate).collect(Collectors.toList()));
            map.put("collectionFee", list.stream().map(PkgMonthDataStatistics::getCollectionFee)
                    .collect(Collectors.toList()));
            map.put("grossFee", list.stream().map(PkgMonthDataStatistics::getGrossFee).collect(Collectors.toList()));
        }
        return ResponseData.andaSuccess(map);
    }

    @ApiOperation(value = "月度收付款数据统计")
    @GetMapping("/collection-payment/{month}")
    public ResponseData getMonthCollectionPaymentData(@PathVariable String month) {
        PkgFinance finance = statisticsService.getMonthCollectionPaymentData(month);
        return ResponseData.andaSuccess(finance == null ? new PkgFinance() : finance);
    }

    @ApiOperation(value = "月度业务数据统计")
    @GetMapping("/business/{month}")
    public ResponseData getMonthBusinessData(@PathVariable String month) {
        List<PkgMonthDataStatistics> list = statisticsService.getMonthBusinessData(month);
        Map<String, Object> map = new HashMap<>();
        map.put("userName", list.stream().map(PkgMonthDataStatistics::getUserName).collect(Collectors.toList()));
        map.put("collectionFee", list.stream().map(PkgMonthDataStatistics::getCollectionFee)
                .collect(Collectors.toList()));
        map.put("orderCount", list.stream().map(PkgMonthDataStatistics::getOrderCount).collect(Collectors.toList()));
        return ResponseData.andaSuccess(map);
    }

    @ApiOperation(value = "月度网点业务量统计")
    @GetMapping("/site-business/{month}")
    public ResponseData getMonthSiteBusinessData(@PathVariable String month) {
        HashMap<String, Object> map = statisticsService.getMonthSiteBusinessData(month);
        return ResponseData.andaSuccess(map);
    }

    @ApiOperation(value = "客户下单月度排名")
    @GetMapping("/order-rank/{month}")
    public ResponseData getLinkmanOrderRank(@PathVariable String month) {
        Map<String, Object> map = statisticsService.getLinkmanOrderRank(month);
        return ResponseData.andaSuccess(map);
    }

    @ApiOperation(value = "客户承运月度排名")
    @GetMapping("/transport-rank/{month}")
    public ResponseData getLinkmanTransportRank(@PathVariable String month) {
        Map<String, Object> map = statisticsService.getLinkmanTransportRank(month);
        return ResponseData.andaSuccess(map);
    }

    @ApiOperation(value = "首页数据")
    @GetMapping("/index")
    public ResponseData getIndexData() {
        IndexDataVO data = statisticsService.getIndexData();
        return ResponseData.andaSuccess(data);
    }


}
